package com.hrm.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.AuditVacationDTO;
import com.hrm.dto.EmpDepDTO;
import com.hrm.entity.EmpVacation;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.entity.Vacation;
import com.hrm.service.EmployeeService;
import com.hrm.service.VacationService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 任旭旺
 * @Date: 2020/5/31 22:19
 * @Description:
 */
@Controller
@RequestMapping("/vacation")
public class VacationController extends BaseController{
    @Autowired
    VacationService vacationService;
    @Autowired
    EmployeeService employeeService;

    /**
     * 跳转假期设置页面
     * @return: String
     */
    @RequestMapping("/vacationConfig.html")
    public String vacationConfig(){
        return "vacationManagement/vacationConfig";
    }

    /**
     * 跳转假期设置页面
     * @return: String
     */
    @RequestMapping("/addVacationForm.html")
    public String addVacationForm(){
        return "vacationManagement/addVacationForm";
    }

    @RequestMapping("/editVacationForm.html")
    public String editVacationForm(){
        return "vacationManagement/editVacationForm";
    }

    @RequestMapping("/applyVacation.html")
    public String applyVacation(){
        return "vacationManagement/applyVacation";
    }

    @RequestMapping("/applyVacationForm.html")
    public String applyVacationForm(){
        return "vacationManagement/applyVacationForm";
    }

    @RequestMapping("/auditVacation.html")
    public String auditVacation(){
        return "vacationManagement/auditVacation";
    }

    @RequestMapping("/auditVacationForm.html")
    public String auditVacationForm(){
        return"vacationManagement/auditVacationForm";
    }
    @RequestMapping("/applyVacationHistory.html")
    public String applyVacationHistory(){
        return"vacationManagement/applyVacationHistory";
    }

    @RequestMapping("/addVacation.html")
    @ResponseBody
    public JsonMsg addVacation(Vacation vacation){
        System.out.println(vacation.getVacationEnd());
        vacationService.insertVacation(vacation);
        return JsonMsg.success();
    }

    @RequestMapping("/editEmployee.html")
    @ResponseBody
    public JsonMsg editEmployee(Employee employee){
        System.out.println(employee.getVacation());
        employeeService.updateEmp(employee);
        return JsonMsg.success();
    }


    @RequestMapping("/vacationList.html")
    @ResponseBody
    public ResponseEntity<Layui> vacationList(MyPage myPage){

        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());

        List<Vacation> vacationList = vacationService.vacationList();

        PageInfo pageInfo = new PageInfo(vacationList,5);

        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());

        return ResponseEntity.ok(layui);
    }

    @RequestMapping("/insertEmpVacation.html")
    @ResponseBody
    public JsonMsg insertEmpVacation(EmpVacation empVacation){
        empVacation.setSubmitTime(DateUtil.parse(DateUtil.now()));
        empVacation.setReasonType(1);
        Long betweenDay = DateUtil.between(empVacation.getBeginTime(),empVacation.getEndTime(),DateUnit.DAY);
        empVacation.setVacationTime(betweenDay.intValue());
        empVacation.setReqStatus("已提交");
        vacationService.insertEmpVacation(empVacation);
        return JsonMsg.success();
    }

    @RequestMapping("/auditVacationList.html")
    @ResponseBody
    public ResponseEntity<Layui> auditVacationList(MyPage myPage){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        Employee employee = (Employee) session.getAttribute("user");
        List<AuditVacationDTO> auditVacationDTOList = vacationService.auditVacationList(employee.getDeptId());
        PageInfo pageInfo = new PageInfo(auditVacationDTOList,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    @RequestMapping("/modifyEmpVacation.html")
    @ResponseBody
    public JsonMsg modifyEmpVacation(EmpVacation empVacation){
        vacationService.modifyEmpVacation(empVacation);
        employeeService.updateSession(session,empVacation.getEmpId());
        return JsonMsg.success();
    }
    @RequestMapping("/applyVacationList.html")
    @ResponseBody
    public ResponseEntity<Layui> applyVacationList(MyPage myPage){

        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());

        Employee employee = (Employee) session.getAttribute("user");
        System.out.println("当前用户id为:"+employee.getId());

        List<EmpVacation> applyVcationList = vacationService.applyVacationList(employee.getId());
        for (EmpVacation empVacation : applyVcationList) {
            System.out.println("假期开始时间为:"+empVacation.getBeginTime());
        }
        PageInfo pageInfo = new PageInfo(applyVcationList,5);

        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());

        return ResponseEntity.ok(layui);
    }
}
